package com.ddxz.middleground.master.controller;

import com.ddxz.middleground.master.dto.MasterHeader;
import com.ddxz.middleground.master.dto.role.RolePage;
import com.ddxz.middleground.master.dto.role.RoleInfo;
import com.ddxz.middleground.master.service.RoleService;
import com.ddxz.tool.web.annotation.DdxzRequestHeader;
import com.ddxz.tool.web.annotation.DdxzRequestParam;
import com.ddxz.tool.web.dto.DdxzResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * class description
 *
 * @author 朱宗刚
 * @version 1.0.0
 * @date 2020/7/29
 */

@RestController
@Api(tags = "角色管理")
public class RoleController {
    @ApiOperation(value = "检索角色", consumes = "application/json", produces = "application/json",
            notes = "检索角色信息，以角色创建时间逆序")
    @RequestMapping(value = "/role/list", method = RequestMethod.GET)
    public DdxzResponse roleList(@DdxzRequestHeader MasterHeader header, @DdxzRequestParam RolePage page) {
        return service.roleList(header, page);
    }

    @ApiOperation(value = "查看角色", consumes = "application/json", produces = "application/json",
            notes = "根据角色标识，获取角色的详细信息")
    @RequestMapping(value = "/role/{roleId}/detail", method = RequestMethod.GET)
    public DdxzResponse roleDetail(@DdxzRequestHeader MasterHeader header, @PathVariable(name = "roleId") String roleId) {
        return service.getRoleDetail(roleId, header);
    }

    @ApiOperation(value = "保存角色", consumes = "application/json", produces = "application/json",
            notes = "将角色信息保存到数据库中，如果id为空则新增角色，否则修改已有角色")
    @RequestMapping(value = "/role/save", method = RequestMethod.POST)
    public DdxzResponse roleSave(@DdxzRequestHeader MasterHeader header, @RequestBody RoleInfo role) {
        return service.saveRole(role, header);
    }

    @ApiOperation(value = "删除角色", consumes = "application/json", produces = "application/json",
            notes = "删除指定的角色，如果角色有绑定的用户则不允许删除")
    @RequestMapping(value = "/role/{roleId}/remove", method = RequestMethod.DELETE)
    public DdxzResponse roleRemove(@DdxzRequestHeader MasterHeader header, @PathVariable(name = "roleId") String roleId) {
        return service.roleRemove(header, roleId);
    }

    @ApiOperation(value = "角色授权", consumes = "application/json", produces = "application/json",
            notes = "将角色与指定的权限进行绑定")
    @RequestMapping(value = "/role/{roleId}/grant", method = RequestMethod.PUT)
    public DdxzResponse roleGrant(@DdxzRequestHeader MasterHeader header,
                                  @PathVariable(name = "roleId") String roleId, @RequestBody List<String> permIdList) {
        return service.roleGrant(header, roleId, permIdList);
    }

    public RoleController(RoleService service) {
        this.service = service;
    }

    private final RoleService service;
}
